<template>
  <div>
    <u-form :label-style="{'font-weight':'bold'}" :model="orderDeliveryForm" class="form" label-width="150" ref="uForm">
      <u-form-item label="物流公司" @click="show=true" prop="logisticsName">
        <u-input type="select" @click="show=true" v-model="orderDeliveryForm.logisticsName" />
      </u-form-item>
      <u-form-item label="物流单号" prop="logisticsNo">
        <u-input v-model="orderDeliveryForm.logisticsNo" />
      </u-form-item>
		<u-form-item label="发货时间" :label-width="150">
		  <u-input input-align="right" type="selects" disabled v-model="orderDeliveryForm.mDeliverTime" @click="timeshow = true" placeholder="请选择发货时间" />
		</u-form-item>
      <div class="submit" @click="update">
        发货
      </div>
    </u-form>
    <u-select confirm-color="#333" @confirm="confirmLogistics" v-model="show" :list="checkedLogistics"></u-select>
   <u-calendar v-model="timeshow" :mode="'date'" @change="onTimeChange"></u-calendar>
  </div>
</template>

<script>
import { getLogisticsChecked, orderSendAsync } from "@/api/order";

export default {
  data() {
    return {
		timeshow:false,
      show: false, //是否显示快递
      rules: {
        logisticsNo: [
          { required: true, message: "发货单号不能为空", trigger: "change" },
        ],
        logisticsId: [
          { required: true, message: "请选择物流公司", trigger: "blur" },
        ],
        mDeliverTime: [
          { required: true, message: "请选择发货日期", trigger: "blur" },
        ]
      },
      checkedLogistics: [], //选中的物流公司集合
      //订单发货

      orderDeliveryForm: {
        logisticsName: "",
        logisticsNo: "", //发货单号
        logisticsId: "", //物流公司
		mDeliverTime:"" //发货日期
      },
	  sn:""
    };
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  onLoad(options) {
	  this.sn = JSON.parse(decodeURIComponent(options.sn));
	  this.init();
  },

  methods: {
	  /**
	   * 更改时间
	   */
	  onTimeChange(e) {
	    this.orderDeliveryForm.mDeliverTime = e.result;
	  },
    //   初始化
    async init() {
      let res = await getLogisticsChecked();
      if (res.data.success) {
        this.checkedLogistics = res.data.result.map((item) => {
          return { value: item.id, label: item.name };
        });
      }
    },
    // 修改/更新数据
    update() {
      this.$refs.uForm.validate(async (valid) => {
        if (valid) {
          let orderDeliveryForm = JSON.parse(
            JSON.stringify(this.orderDeliveryForm)
          );
          delete orderDeliveryForm.logisticsName;
          uni.showLoading({
            title: "加载中",
            mask: "true",
          });

          let res = await orderSendAsync(this.sn, orderDeliveryForm);
          if (res.data.success) {
            uni.showToast({
              title: "发货成功",
              duration: 2000,
              icon: "none",
            });

            setTimeout(() => {
              uni.navigateBack();
            }, 500);
          }
          uni.hideLoading();
        }
      });
    },

    // 确定地址
    confirmLogistics(val) {
      this.orderDeliveryForm.logisticsId = val[0].value;
      this.orderDeliveryForm.logisticsName = val[0].label;
    },
  },
};
</script>

<style scoped lang="scss">
@import "../../style.scss";
.form {
  padding: 32rpx;
  background: #fff;
}
// #ifdef MP-WEIXIN
/deep/ .u-form{
  padding: 32rpx;
}
// #endif
</style>